package ${pkg + ".controller"};

import ${daoPkg + ".domain." + domain};
import ${daoPkg + ".dto."+ domain + "DTO"};
import ${daoPkg + ".vo."+ domain + "VO"};
import ${daoPkg + ".service." + domain + "Service"};
import cn.ezios.huo.common.mvc.Ret;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("${rootPath}")
@RequiredArgsConstructor
public class ${domain + "Controller"} {

    private final ${domain + "Service"} ${domainVarName + "Service"};

    @PostMapping("")
    public Ret add(@RequestBody ${domain} ${domainVarName}) {
        boolean save = ${domainVarName + "Service"}.save(${domainVarName});
        return save ? Ret.ok() : Ret.error();
    }

    @PutMapping("")
    public Ret update(@RequestBody ${domain} ${domainVarName}) {
        boolean update = ${domainVarName + "Service"}.updateById(${domainVarName});
        return update ? Ret.ok() : Ret.error();
    }

    @GetMapping("{id}")
    public Ret get(@PathVariable("id") String id) {
        ${domain} ${domainVarName} = ${domainVarName + "Service"}.getById(id);
        return ${domainVarName} != null ? Ret.ok(${domainVarName}) : Ret.error("未找到");
    }

    @GetMapping("page")
    public Ret page(@RequestParam(value = "current", defaultValue = "1") Integer current,
    @RequestParam(value = "size", defaultValue = "10") Integer size,${domain}DTO dto) {
       IPage ${r"<"}${domain}VO${r">"} page = ${domainVarName + "Service"}.queryPage(current,size,dto);
            return Ret.ok(page);
    }

    @DeleteMapping("{id}")
    public Ret delete(@PathVariable("id") String id) {
        boolean remove = ${domainVarName + "Service"}.removeById(id);
        return remove ? Ret.ok() : Ret.error();
    }
}
